package com.asiainfo.indb.view;

import com.asiainfo.indb.PanelUtil;
import com.asiainfo.indb.listener.PlaceholderListener;
import com.asiainfo.indb.model.DbType;
import com.asiainfo.indb.model.FileGather;
import com.asiainfo.indb.model.InDbGather;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

/**
 * @author zym
 * @date 2019-10-17 11:00
 */
public class DbPanel extends BaseTitlePanel implements ActionListener {

    private static final String ORACLE_URL = "jdbc:oracle:thin:@127.0.0.1:1521:dbname";
    private static final String MYSQL_URL = "jdbc:mysql://localhost:3306/dbname";
    private PlaceholderListener urlPlaceholder;
    private JTextField urlField;
    private JTextField userField;
    private JPasswordField passwordField;
    private ButtonGroup dbTypeGroup;

    public DbPanel(InDbPanel parentPanel) {
        super(parentPanel);
    }

    @Override
    protected String getPanelTitle() {
        return "数据库配置";
    }

    @Override
    protected void render(JPanel container) {
        // 1、数据库类型
        // 2、url连接串
        // 3、用户名和密码
        container.setLayout(new GridLayout(3, 1, 2, 5));

        renderDbType(container);
        renderUrl(container);
        renderUserInfo(container);
    }

    private void renderUserInfo(JPanel container) {
        userField = new JTextField();

        passwordField = new JPasswordField();
        passwordField.setEchoChar('*');

        JPanel panel = PanelUtil.generateTwoGridInputPanel("用户名：", userField, "密码：", passwordField, 4);
        container.add(panel);
    }

    private void renderUrl(JPanel container) {
        urlField = new JTextField();

        urlPlaceholder = new PlaceholderListener(urlField, ORACLE_URL);
        urlField.addFocusListener(urlPlaceholder);
        JPanel panel = PanelUtil.generateOneGridInputPanel("连接串：", urlField, 4);

        // 加到父级
        container.add(panel);
    }

    private void renderDbType(JPanel container) {
        JPanel panel = new JPanel(new BorderLayout());

        JLabel label = new JLabel("数据库类型：");
        label.setPreferredSize(new Dimension(PanelUtil.computeLabelWidth(6), 0));
        panel.add(label, BorderLayout.WEST);

        // 单选框组
        JRadioButton radioButton;
        dbTypeGroup = new ButtonGroup();
        JPanel radioPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        for (Object[] type : DbType.TYPES) {
            radioButton = new JRadioButton((String) type[0], (boolean) type[1]);
            radioButton.addActionListener(this);

            dbTypeGroup.add(radioButton);
            radioPanel.add(radioButton);
        }
        panel.add(radioPanel, BorderLayout.CENTER);

        // 加到父级
        container.add(panel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object target = e.getSource();
        if (target instanceof JRadioButton && urlPlaceholder != null) {
            JRadioButton btn = (JRadioButton) target;
            if ("mysql".equals(btn.getText())) {
                urlPlaceholder.freshPlaceholder(MYSQL_URL);
            } else {
                urlPlaceholder.freshPlaceholder(ORACLE_URL);
            }
        }
    }

    @Override
    public InDbGather gather() {
        InDbGather gather = new InDbGather();
        // 连接串
        if (urlField.getForeground() == Color.BLACK) {
            gather.put("DB_URL", urlField.getText());
        } else {
            PanelUtil.alert(getTopContainer(), "请输入url连接串");
            urlField.requestFocus();
            return null;
        }

        if (checkAndSetField("请输入数据库用户名", userField, "DB_USER", gather)
                && checkAndSetField("请输入数据库密码", passwordField, "DB_PASSWORD", gather)) {
            if (dbTypeGroup.getButtonCount() > 0) {
                // 获取数据库类型
                AbstractButton selBtn;
                Enumeration<AbstractButton> btns = dbTypeGroup.getElements();
                while (btns.hasMoreElements()) {
                    selBtn = btns.nextElement();
                    if (selBtn.isSelected()) {
                        gather.put("DB_TYPE", selBtn.getText());
                        break;
                    }
                }

                return gather;
            }
        }

        return null;
    }

    @Override
    public void initByFile(FileGather gather) {
        initField("DB_URL", urlField, gather);
        initField("DB_USER", userField, gather);
        initField("DB_PASSWORD", passwordField, gather);

        if (gather != null) {
            String dbType = gather.get("DB_TYPE");
            if (StringUtils.isNotBlank(dbType) && dbTypeGroup != null && dbTypeGroup.getButtonCount() > 0) {
                AbstractButton selBtn;
                Enumeration<AbstractButton> btns = dbTypeGroup.getElements();
                while (btns.hasMoreElements()) {
                    selBtn = btns.nextElement();
                    if (dbType.equals(selBtn.getText())) {
                        selBtn.setSelected(true);
                        break;
                    }
                }
            }
        }
    }
}